package com.wtx.chapter03;

import java.util.Stack;

/**
 * @description: leetcode 20号题
 * @author: wtx
 * @createDate: 2020/5/14
 */
public class Solution {

    public boolean isValid(String s){

        Stack<Character> stack = new Stack<>();

        for (int i = 0; i < s.length(); i++) {

            char c = s.charAt(i);
            if ( c=='(' || c=='[' || c=='{' )
                stack.push(c);
            else {
                if (stack.isEmpty())
                    return false;
                Character topChar = stack.pop();
                if ( c==')' && topChar!='(' )
                    return false;
                if ( c==']' && topChar!='[' )
                    return false;
                if ( c=='}' && topChar!='{' )
                    return false;
            }
        }

        //记得对栈判空
        return stack.isEmpty();
    }
}
